#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os
import time
import unittest

from common.BrowserDriver import BrowserDriver
from utils.report import HTMLTestRunner

def create_suite():
    TestSuite = unittest.TestSuite()  # 测试集
    test_dir = 'tests'

    discover = unittest.defaultTestLoader.discover(
        start_dir=test_dir,
        pattern='test_*.py',
        top_level_dir=None
    )

    for test_case in discover:
        TestSuite.addTests(test_case)
    return TestSuite

def report():
    dirname = os.getcwd() + '\\reports\\' + time.strftime("%Y-%m-%d")
    if not os.path.exists(dirname):
        os.makedirs(dirname)
    # 需要查看每段时间的测试报告，可以这样写：

    report_name = dirname+'\\'+time.strftime("%H_%M_%S_")+'result.html'
    # report_name = './report-front/result.html'
    print(report_name)
    return report_name



if __name__ == '__main__':
    # 打开浏览器
    driver = BrowserDriver()

    TestSuite = create_suite()
    fp = open(report(), 'wb')
    Runner = HTMLTestRunner(
        stream=fp,
        title='测试报告',
        description='测试用例执行情况'
    )
    Runner.run(TestSuite)
    fp.close()
    # 关闭浏览器
    driver.quit_browser()

